package com.ihr360.payroll.model.dto.attendance.plan;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ihr360.authority.sharing.entityhandler.UserResourceHandler;
import com.ihr360.commons.entityhandler.DateHandler;
import com.ihr360.commons.entityhandler.Ihr360ContextHandler;
import com.ihr360.payroll.model.dto.salary.meta.PayrollConfig;
import com.ihr360.payroll.model.vo.PayrollConfigVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.ArrayList;

/**
 * 考勤计薪方案
 *
 * @author tomtao
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"companyId","name"}))
@EntityListeners(value = {UserResourceHandler.class, Ihr360ContextHandler.class, DateHandler.class})
public class AttendancePlan extends AbstractAttendancePlan {
    public static final class Field{
        public final static String ID = "id";
        public final static String IS_SYSTEM_TEMPLATE = "isSystemTemplate";
        public final static String NAME = "name";

    }

    @Column(length =40,nullable = false,name = "companyId")
    private String companyId;

    /**
     * 方案引用的规则中的工资表必须与此一致 （待讨论，与CNB设计不一致）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false)
    private PayrollConfig payrollConfig;

    private boolean isSystemTemplate = false;

    @JsonIgnore
    public PayrollConfigVo getPayrollConfigVo() {
        if (payrollConfig != null) {
            return new PayrollConfigVo(payrollConfig.getId(), payrollConfig.getName(), new ArrayList<>(),payrollConfig.getSalaryAdjustFormula());
        }
        return null;
    }

}
